#include "LoginInterfaz.h"
#include <string>

using std::string;


const unsigned int WIDTHINICIAL= 424;
const unsigned int HEIGTHINICIAL = 224;
const char RUTAPANTALLAINICIAL[] = "Imagenes/PantallaInicial.png";
const char RUTALOGIN[] = "Imagenes/Login.png";
const char RUTAESPERA[] = "Imagenes/LoginEspera.png";
const char RUTADATOSINCORRECTOS[] = "Imagenes/fondoDatosIncorrecto.png";
const char RUTAUSUARIOEXISTENTE[] = "Imagenes/fondoUsuarioExistente.png";

LoginInterfaz::LoginInterfaz(Conexion* sock, const Gtk::Main& kit){
	ancho = WIDTHINICIAL;
	alto = HEIGTHINICIAL;
	ingresaUsuario = true;
	opcion = true;
	pantallaInicial = true;
	stream = sock;
	success = false;
	this->kit=kit;
	user = new StringInput();
	pass = new StringInput();
}

LoginInterfaz::~LoginInterfaz() {
	delete user;
	delete pass;
}


void LoginInterfaz::ubicarMouse(unsigned int x, unsigned int y){
	posXMouse = x;
	posYMouse = y;
}

void LoginInterfaz::logInit(){
	init("Login");
	fondo = load_image(RUTAPANTALLAINICIAL);
}

std::string LoginInterfaz::getPass(){
	return pass->getStringValue();
}

void LoginInterfaz::dibujar(){
	apply_surface(0,0, fondo, screen, NULL);
	if (!pantallaInicial && !success){
		user->show(205, 40, screen);
		pass->showHidden(205, 105, screen);
	}
	SDL_Flip(screen);
}

void LoginInterfaz::handleInput(SDL_Event& event){
    if (event.key.keysym.sym == SDLK_TAB){
        ingresaUsuario = !ingresaUsuario;
    }else{
		if (ingresaUsuario){
			user->handle_input(event);
		}else{
			pass->handle_input(event);
		}
    }
}

void LoginInterfaz::setIngresaUsuario(){
	if (posXMouse < 380 && posXMouse > 205){
		if (posYMouse > 30){
			if (posYMouse < 75){
				ingresaUsuario = true;
			}else if (posYMouse > 90 && posYMouse < 135){
				ingresaUsuario = false;
			}
		}
	}
}

bool LoginInterfaz::handleClick(){
	if (pantallaInicial){
		setIngresaOpcion();
	}else{
		if (posXMouse > 135 && posYMouse > 145 && posXMouse < 290 && posYMouse < 190){
			return 	enviarDatos();
		}else{
			setIngresaUsuario();
		}
	}
	return false;
}

void LoginInterfaz::setIngresaOpcion(){
	if (posYMouse > 90 && posYMouse < 170){
		if (posXMouse > 30 && posXMouse < 190){
			opcion = true;
			pasarALogin();
		}else if (posXMouse > 220 && posXMouse < 380){
			opcion =false;
			pasarALogin();
		}
	}
}

void LoginInterfaz::pasarALogin(){
	SDL_FreeSurface(fondo);
	fondo = load_image(RUTALOGIN);
	pantallaInicial = false;
}

void LoginInterfaz::pasarAInicial(){
	SDL_FreeSurface(fondo);
	fondo = load_image(RUTAPANTALLAINICIAL);
	ingresaUsuario = true;
	opcion = true;
	pantallaInicial = true;
	user->emptyValue();
	pass->emptyValue();
}

Json::Value LoginInterfaz::mostrarLobby(){
	Mensaje msj;
	Json::Value partidasJson = msj.convertJson(stream->receive());
	Glib::RefPtr < Gtk::Builder >  builder = Gtk::Builder::create_from_file("lobby.glade");
	
	Lobby *winlobby;
	
	builder->get_widget_derived("winlobby", winlobby);
	winlobby->setPartidasJson(partidasJson);
	winlobby->actualizarPartidas();
	kit.run(*winlobby);
	
	return winlobby->getRespuesta();
}


bool LoginInterfaz::pedirLogin(){
	SDL_EnableUNICODE(SDL_ENABLE);
	bool quit = false;
	SDL_Event event;
	dibujar();
	while(quit == false){
		while(SDL_PollEvent(&event)){
			//If the user has Xed out the window
			if (event.type == SDL_QUIT){
				//Quit the program
				quit = true;
			}else if (event.type == SDL_KEYDOWN){
				handleInput(event);
			}else if (event.type == SDL_MOUSEMOTION){
				ubicarMouse(event.motion.x, event.motion.y);
			}else if (event.type == SDL_MOUSEBUTTONUP){
				quit = handleClick();
			}
			dibujar();
		}
	}

	SDL_FreeSurface(fondo);
	SDL_FreeSurface(screen);
	SDL_EnableUNICODE(SDL_DISABLE);
	return quit;
}


Json::Value LoginInterfaz::getJSonValues(){
	Json::Value loginJson(Json::objectValue);
	loginJson["nueva"] = !opcion;
	loginJson["usuario"] = user->getStringValue();
	loginJson["clave"] = pass->getStringValue();
	return loginJson;
}


bool LoginInterfaz::enviarDatos(){
	bool retorna = false;
	string mensaje = "";
	Json::Value loginJson(Json::objectValue);
	loginJson = getJSonValues();
	mensaje = loginJson.toStyledString();
	stream->send(mensaje);
	mensaje = "";
	mensaje = stream->receive();
	if (mensaje.compare("0") == 0){
		success = true;
		retorna = true;
	}else if (mensaje.compare("1") == 0){
		mostrarDatosIncorrectos();
	}else if (mensaje.compare("2") == 0){
		mostrarUsuarioExistente();
	}
	return retorna;
}

void LoginInterfaz::mostrarDatosIncorrectos(){
	SDL_Surface* fondoNuevo = SDL::load_image(RUTADATOSINCORRECTOS);
	apply_surface(0,0, fondoNuevo, screen, NULL);
	SDL_Flip(screen);
	SDL_Delay(1000);
	SDL_FreeSurface(fondoNuevo);
	pasarAInicial();
}

void LoginInterfaz::mostrarUsuarioExistente(){
	SDL_Surface* fondoNuevo = SDL::load_image(RUTAUSUARIOEXISTENTE);
	apply_surface(0,0, fondoNuevo, screen, NULL);
	SDL_Flip(screen);
	SDL_Delay(1000);
	SDL_FreeSurface(fondoNuevo);
	pasarAInicial();
}

bool LoginInterfaz::logueoExitoso(){
	return success;
}

void LoginInterfaz::pasarAEspera(){
	SDL_FreeSurface(fondo);
	fondo = load_image(RUTAESPERA);
	apply_surface(0,0, fondo, screen, NULL);
	SDL_Flip(screen);
}


bool LoginInterfaz::loguear(){
	bool exito;
	logInit();
	pedirLogin();
	exito = logueoExitoso();
	if (exito){
		SDL_Quit();
	}
	return exito;
}

void LoginInterfaz::reconnect(Conexion* sd){
	stream = sd;
	enviarDatos();
}


